package cn.zyjblogs.starter.feign.hystrix;

import cn.zyjblogs.starter.common.entity.context.BaseContext;
import cn.zyjblogs.starter.common.entity.dto.ContextDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;

/**
 * @author zhuyijun
 */
public class BaseContextHystrixCallable<K> implements Callable<K> {
    private static final Logger log = LoggerFactory.getLogger(BaseContextHystrixCallable.class);
    private final Callable<K> actual;
    private final ContextDto contextDto;

    public BaseContextHystrixCallable(Callable<K> actual) {
        this.actual = actual;
        this.contextDto = BaseContext.get();
    }

    @Override
    public K call() throws Exception {
        K k;
        try {
            BaseContext.set(contextDto);
            log.debug("发生熔断，当前Context:{}", this.contextDto.toString());
            k = this.actual.call();
        } finally {
            BaseContext.clear();
        }
        return k;
    }
}
